implementation strings *1200

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <utility>
#include <vector>
#include <iostream>
#include <vector>
#include <numeric>
#define ll long long
#include <iomanip>
using namespace std;

void solve()
{
    string s;
    cin >> s;
    // x=0.0982;
    // y=x;
    // if(y<0)
    // {
    //     y=-y;
    // }
    // y = floor(y * 100) / 100;
    // y = round(y * 100) / 100;
    // cout<<s;
    // string s=to_string(y);
    // cout<<s<<endl;
    string s1="";
    string s2="";
    for(int i=0;i<s.length();i++)
    {
        if(s[i]=='.')
        {
            if(i+2<s.length())
            {
                s2+=s[i+1];
                s2+=s[i+2];
                break;
            }
            else if(i+1<s.length())
            {
                s2+=s[i+1];
                s2+='0';
                break;
            }
        }
        s1+=s[i];
    }
    // cout<<s1<<endl<<s2<<endl;
    if(s[0]=='-')
    {
        s1.erase(0, 1);
    }
    int cnt=0;
    string sfrount="";
    for(int i=s1.length()-1;i>=0;i--)
    {
        sfrount+=s1[i];
        cnt++;
        if(cnt==3)
        {
            cnt=0;
            
        sfrount+=',';
        }
        
    }
    reverse(sfrount.begin(), sfrount.end());
    if(s1.length()%3==0)
    {
        sfrount.erase(0, 1);
    }
    if(s[0]=='-')
    {
        // s1.erase(0, 1);
    cout<<"(";
    }
    cout<<"$";
    if(sfrount!="")
    {
    cout<<sfrount;
    }
    else{
        cout<<"0";
    }
    cout<<".";
    if(s2=="")
    {
        // cout<<"hi";
        cout<<"00";
    }
    
    cout<<s2;
    if(s[0]=='-')
    {
        cout<<")"<<endl;
    }
    // cout<<"hi";
    // cout<<s2<<"hi";
    
}

int main()
{
    ll i = 1;
    // cin>>i;
    while (i > 0)
    {
        solve();
        i--;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall